package task;

import javafx.application.Platform;
import model.SharedObject;

import java.util.Random;

/**
 * 更新UI任务
 * @author wxhntmy
 */
public class RefreshUITask implements Runnable {

    /**
     * 任务名称
     */
    private String name;

    /**
     * 共享对象
     */
    private SharedObject sharedObject;

    /**
     * 构造函数
     *
     * @param name 任务名称
     * @param sharedObject 线程共享对象
     */
    public RefreshUITask(String name, SharedObject sharedObject) {
        this.name = name;
        this.sharedObject = sharedObject;
    }

    @Override
    public void run() {
        System.out.println(this.toString() + " is running!");
        Random random = new Random();
        Platform.runLater(() -> {
            sharedObject.getUIDisplayLabel().setText("更新UI（显示随机数）：" + random.nextInt(1000));
        });

        System.out.println(this.toString() + " is end!");
    }

    /**
     * 获取任务名称
     *
     * @return 任务名称
     */
    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "MyTask [ name = " + name + " ]";
    }
}
